<template>
  <div class="card-container">
    <el-form :model="form" :rules="formRules" ref="formRef" label-width="120px" style="width: 100%;" label-position="right">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>编辑账号</span>
        </div>
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="选择角色：" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option v-for="item in roleList" :key="item.id" :label="item.title" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
      </el-card>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存修改</el-button>
        <!-- <el-button @click="resetForm">重置</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getRoleList, editAccount, getAccountDetail } from '@/api/permission'

export default {
  name: 'editAccount',
  data() {
    return {
      form: {},
      formRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        role_id: [{ required: true, message: '请选择账号角色', trigger: 'change' }],
      },
      roleList: [],
    }
  },
  created() {
    this.getAccountDetail()
  },
  methods: {
    async getAccountDetail() {
      const res = await getAccountDetail(this.$route.query.id)
      res.data.password = ''
      this.form = res.data
      this.getRoleList()
    },
    async getRoleList() {
      const res = await getRoleList()
      if (res.status != 1) return this.$message.error(res.msg)
      // this.$message.success(res.msg)
      console.log(res.data)
      this.roleList = res.data
    },
    async submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return false
        const res = await editAccount(this.form)
        if (res.status != 1) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        this.closePage()
      })
    },
    resetForm() {
      this.$refs.formRef.resetFields()
      this.form = this.$options.data().form
    },
    async closePage() {
      let view = this.$route
      await this.$store.dispatch('tagsBar/delRoute', view)
      this.$router.push('/accountList')
    },
  },
}
</script>

<style lang="scss" scoped>
// .el-form-item {
//   // width: 420px;
// }
::v-deep .el-input__inner {
  // margin-left: 20px;
  height: 36px;
  width: 420px;
}
::v-deep .el-checkbox {
  height: auto !important;
}
.el-card {
  border-radius: 8px;
}
::v-deep .el-card__header {
  font-size: 14px;
  color: #555;
  font-weight: bold;
}
::v-deep .el-form-item__label {
  padding-right: 12px;
}
::v-deep .el-range-separator {
  line-height: 28px;
}
::v-deep .el-range-input {
  cursor: pointer;
}
.radio-box {
  display: flex;
  align-items: center;
  height: 36px;
  width: 600px;
}
.tips {
  color: #999;
  margin-top: 10px;
  font-size: 12px;
}
</style>
